<?php

class model
{
    public $db;
    function connect() {
        $dbms='mysql';
        $host='39.106.64.16';
        $dbName='cuisine';
        $user='root';
        $pass='Zuolinyouqian!';
        $dsn="$dbms:host=$host;dbname=$dbName";

        try {
            $this->db = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));
        } catch (PDOException $e) {
            die ("Error!: " . $e->getMessage() . "<br/>");
        }
    }

    /**
     * check email exist
     * @param $email
     * @return string
     */
    function selectUserByEmail($email) {
        $stmt = $this->db->prepare("select * from user where email = :email");
        $stmt->bindParam('email',$email);
        $stmt->execute();
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        if ($row) {
            return true;
        }
        return false;
    }

    /**
     * check the email and password
     * @param $email
     * @param $password
     * @return string
     */
    function checkLogin($email,$password) {
        $stmt = $this->db->prepare("select * from user where email = :email and password = :password");
        $stmt->bindParam('email',$email);
        $stmt->bindParam('password',$password);
        $stmt->execute();
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        return $row;
    }

    /**
     * @param $email
     * @param $password
     * @param $userName
     * @return bool
     */
    function register($email,$password,$userName) {
        $stmt = $this->db->prepare("insert into `user`(`email`,`password`,`user_name`) values (:email,:password,:userName)");
        $stmt->bindParam('email',$email);
        $stmt->bindParam('password',$password);
        $stmt->bindParam('userName',$userName);
        $stmt->execute();
        $count = $stmt->rowCount();
        if (!$count) {
            return false;
        }
        return true;
    }

    /**
     * getAllItems;
     * @return mixed
     */
    function getAllCuisine(){
        $stmt = $this->db->prepare("select * from `cuisines`");
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    /**
     * save cuisine
     * @param $name
     * @param $desc
     * @param $pic
     * @return bool
     */
    function saveCuisine($name,$desc,$pic) {
        $stmt = $this->db->prepare("insert into `cuisines`(`name`,`desc`,`pic`) values (:name,:desc,:pic)");
        $stmt->bindParam('name',$name);
        $stmt->bindParam('desc',$desc);
        $stmt->bindParam('pic',$pic);
        $stmt->execute();
        $count = $stmt->rowCount();
        if (!$count) {
            return false;
        }
        return true;
    }

}